<?php
// +----------------------------------------------------------------------
// | 区域 模型
// +----------------------------------------------------------------------
namespace app\wechat\model;

use think\Model;

class Area extends Model{
    /**
     * 获取所有分区
     * @param int $id 选中的ID
     * @return string
     */
    public static function getArea(){
        $rows = self::column('id,pid,areatype,areaname,tag');
        $rows = getTree($rows,0);
        $arr = getAreaArr($rows);
        return $arr;
    }
    /**
     * 获取指定条件下的所有区域
     * @param int $id 选中的ID
     * @return string
     */
    public function getChildArea($id){
        $group=[];
        $list = self::where('pid',$id)->column('id');
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $group[$key]['id']=$value;
                $group[$key]['child']=$this->getChildArea($value);
            }
        }
        return $group;
    }
    /**
     * 获取两级区域
     * @param int $id 选中的ID
     * @return string
     */
    public static function getTwoArea(){
        $rows = self::column('id,pid,areatype,areaname,tag');
        $rows = getTree($rows,0);
        $arr = [];
        foreach ($rows as $k1 => $v1){
            $arr[$k1]['areaid'] = $v1['id'];
            $arr[$k1]['areaname'] = $v1['areaname'];
            $child = [];
            foreach ($v1['child'] as $k2 => $v2){
                $child[$k2]['areaid'] = $v2['id'];
                $child[$k2]['areaname'] = $v2['areaname'];
            }
            $arr[$k1]['child'] = $child;
        }
        return $arr;
    }
    /**
     * 获取分区背景图
     * @param int $id 选中的ID
     * @return string
     */
    public function getAreaBgimg($id){
        $rows = self::get($id);
        return $rows->bgimg;
    }
    /**
     * 查询区域名称数组
     * @return array
     */
    public function getAllAreaName(){
        $rows = self::column('id,pid,areatype,areaname');
        $rows = getTree($rows,0);
        $arr = areaName($rows);
        return $arr;
    }
}